package club.lovety.es.service;

import club.lovety.es.client.ESClientUtils;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.SortOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.stream.Stream;

/**
 * 这里写文档描述
 *
 * @author smc
 * @date 2018-08-29 10:46
 * @since
 */
@Service
public class SearchService {

    private static final Logger log = LoggerFactory.getLogger(SearchService.class);

    public Object searchOne() {

        Client client = ESClientUtils.getClient();
        SearchResponse response = client.prepareSearch("log").addSort("id", SortOrder.DESC).get();
        SearchHits searchHits = response.getHits();
        long total = searchHits.totalHits;
        log.info("total: {}", total);

        Stream.of(searchHits.getHits()).forEach(e -> {
            Map<String, Object> source = e.getSource();
            source.entrySet().forEach(s -> {
                log.info("key:{},value:{}", s.getKey(), s.getValue());
            });
        });
        return searchHits;
    }

    public Object search(){
        Client client = ESClientUtils.getClient();
        SearchResponse response =  client.prepareSearch("filebeat-6.3.2-2018.08.31").get();

        SearchHits searchHits = response.getHits();
        long total = searchHits.totalHits;
        log.info("total: {}", total);
        Stream.of(searchHits.getHits()).forEach(e -> {
            Map<String, Object> source = e.getSource();
            source.entrySet().forEach(s -> {
                log.info("key:{},value:{}", s.getKey(), s.getValue());
            });
        });

        return null;
    }


}
